<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>接口说明</title>
    <link href="/static/bootstrap-5.3.0-alpha1-dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        .card {
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
            border: none;
            border-radius: 12px;
            overflow: hidden;
        }

        .card-header {
            background: #fff;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            padding: 1rem 1.25rem;
        }

        .card-header h5 {
            color: #2c3e50;
            font-weight: 600;
            font-size: 1.1rem;
            letter-spacing: 0.3px;
        }

        .card-body {
            padding: 1.5rem;
        }

        .card-body h6 {
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            font-size: 1rem;
        }

        .card-body h6::before {
            content: "";
            display: inline-block;
            width: 4px;
            height: 16px;
            background: linear-gradient(to bottom, #2196F3, #1976D2);
            margin-right: 8px;
            border-radius: 2px;
        }

        .current-config {
            padding: 8px 12px;
            background: linear-gradient(145deg, #f8fafc, #f1f5f9);
            border-radius: 4px;
            border: 1px solid rgba(0,0,0,0.03);
        }

        .current-config code {
            margin-left: 8px;
            padding: 2px 6px;
            border-radius: 3px;
            color: inherit;
            background: rgba(255,255,255,0.9);
            border: 1px solid rgba(0,0,0,0.04);
        }

        .input-group {
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
            border-radius: 6px;
            overflow: hidden;
        }

        .input-group .form-control {
            border: 1px solid rgba(0,0,0,0.05);
            padding: 0.6rem 1rem;
            font-size: 0.95rem;
        }

        .input-group .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: none;
        }

        .input-group .btn-primary {
            padding-left: 1.5rem;
            padding-right: 1.5rem;
            font-weight: 500;
            background: linear-gradient(145deg, #2196F3, #1976D2);
            border: none;
            transition: all 0.3s ease;
        }

        .input-group .btn-primary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
        }

        .config-info {
            margin-bottom: 1rem;
        }

        .config-info p {
            position: relative;
            padding-left: 12px;
            color: #64748b;
            font-size: 0.9rem;
        }

        .list-unstyled li {
            color: #475569;
            display: flex;
            align-items: center;
            font-size: 0.95rem;
        }

        .list-unstyled li::before {
            content: "•";
            color: #94a3b8;
            margin-right: 8px;
            font-size: 1.2em;
        }

        .version-info {
            display: inline-flex;
            align-items: center;
            padding: 0.35rem 0.75rem;
            background: linear-gradient(145deg, #f8fafc, #f1f5f9);
            border-radius: 4px;
            color: #475569;
            font-size: 0.9rem;
            border: 1px solid rgba(0,0,0,0.03);
            box-shadow: 0 2px 4px rgba(0,0,0,0.02);
        }

        .version-info i {
            color: #2196F3;
        }

        /* 美化滚动条 */
        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track {
            background: #f1f5f9;
        }

        ::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 4px;
        }

        ::-webkit-scrollbar-thumb:hover {
            background: #94a3b8;
        }
    </style>
</head>
<body>
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">接口说明</h5>
        </div>
        <div class="card-body">
            <h6>基础域名</h6>
            <div class="current-config mb-2">
                <small class="text-muted fw-500">当前配置：</small>
                <code id="currentBaseUrl">未配置</code>
                <button class="btn btn-link btn-sm text-danger p-0 ms-2 delete-config" style="display:none;">
                    <small>删除</small>
                </button>
            </div>
            <div class="input-group mb-3">
                <input type="text" class="form-control" id="baseUrlInput" 
                       placeholder="请输入基础域名，如: http://api.example.com">
                <button class="btn btn-primary" type="button" id="saveBaseUrl">保存</button>
            </div>
            <div class="config-info small text-muted mt-2">
                <p class="mb-1">配置的域名将用于所有接口请求</p>
                <p class="mb-1">配置信息保存在浏览器本地存储中</p>
                <p class="mb-0">清除浏览器缓存会导致配置丢失</p>
            </div>

            <h6>接口规范</h6>
            <ul class="list-unstyled">
                <li>接口采用 RESTful 规范设计</li>
                <li>请求与响应数据格式均为 JSON</li>
                <li>请求方法包括: GET、POST、PUT、DELETE</li>
                <li>时间格式统一为: YYYY-MM-DD HH:mm:ss</li>
            </ul>

            <h6 class="mt-4">版本控制</h6>
            <div class="version-info">
                <i class="fa fa-code-fork me-2"></i>
                当前版本：v1.0.0
            </div>
        </div>
    </div>

    <script src="/static/jquery-3.5.1/jquery-3.5.1.min.js"></script>
    <script>
        $(document).ready(function() {
            // 更新显示当前配置
            function updateCurrentConfig() {
                const currentUrl = localStorage.getItem('api_base_url');
                const display = $('#currentBaseUrl');
                const input = $('#baseUrlInput');
                const deleteBtn = $('.delete-config');
                display.removeClass('text-success text-danger');
                if (currentUrl) {
                    display.text(currentUrl)
                           .addClass('text-success')
                           .css('color', '#198754');
                    input.val(currentUrl);
                    deleteBtn.show();
                } else {
                    display.text('未配置')
                           .addClass('text-danger')
                           .css('color', '#dc3545');
                    input.val('');
                    deleteBtn.hide();
                }
            }

            // 初始显示当前配置
            updateCurrentConfig();

            // 保存baseUrl
            $('#saveBaseUrl').on('click', function() {
                const baseUrl = $('#baseUrlInput').val().trim();
                console.log('准备保存的域名:', baseUrl);
                if (!baseUrl) {
                    showAlert('请输入基础域名', 'danger');
                    return;
                }

                try {
                    // 验证URL格式
                    new URL(baseUrl);
                    // 保存到localStorage
                    try {
                        window.localStorage.setItem('api_base_url', baseUrl);
                        console.log('保存成功');
                        showAlert('基础域名保存成功', 'success');
                        // 更新显示
                        updateCurrentConfig();
                    } catch (storageError) {
                        console.error('保存到localStorage失败:', storageError);
                        showAlert('保存失败，请检查浏览器存储权限', 'danger');
                    }
                } catch (e) {
                    console.error('URL格式验证失败:', e);
                    showAlert('请输入有效的URL', 'danger');
                }
            });

            // 添加回车键保存功能
            $('#baseUrlInput').on('keypress', function(e) {
                if (e.which === 13) {
                    $('#saveBaseUrl').click();
                }
            });

            function showAlert(message, type) {
                try {
                    // 获取父窗口中的 toast 元素
                    const parentDoc = window.parent.document;
                    let toast = parentDoc.getElementById('toast');
                    
                    // 如果找不到 toast 元素，创建一个新的
                    if (!toast) {
                        const toastContainer = parentDoc.createElement('div');
                        toastContainer.className = 'toast-container position-fixed top-0 end-0 p-3';
                        toastContainer.innerHTML = `
                            <div id="toast" class="toast align-items-center border-0" role="alert" aria-live="assertive" aria-atomic="true">
                                <div class="d-flex">
                                    <div class="toast-icon d-flex align-items-center justify-content-center">
                                        <i class="fa"></i>
                                    </div>
                                    <div class="toast-body"></div>
                                    <button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
                                </div>
                            </div>
                        `;
                        parentDoc.body.appendChild(toastContainer);
                        toast = parentDoc.getElementById('toast');
                    }
                    
                    const toastBody = toast.querySelector('.toast-body');
                    
                    // 设置样式和内容
                    toast.className = `toast align-items-center text-white bg-${type} border-0`;
                    toastBody.textContent = message;
                    
                    // 使用 Bootstrap Toast 实例显示提示
                    const bsToast = new window.parent.bootstrap.Toast(toast, {
                        delay: 3000
                    });
                    bsToast.show();
                } catch (error) {
                    // 如果出错，回退到使用 alert
                    console.error('Toast 显示失败:', error);
                    alert(message);
                }
            }

            // 检查localStorage是否可用
            function checkStorage() {
                try {
                    localStorage.setItem('test', 'test');
                    localStorage.removeItem('test');
                    return true;
                } catch (e) {
                    console.error('localStorage不可用:', e);
                    return false;
                }
            }

            // 初始检查
            if (!checkStorage()) {
                showAlert('浏览器存储功能不可用，配置将无法保存', 'warning');
            }

            // 删除配置
            $('.delete-config').on('click', function() {
                if (confirm('确定要删除当前配置吗？')) {
                    try {
                        localStorage.removeItem('api_base_url');
                        updateCurrentConfig();
                        showAlert('配置已删除', 'success');
                    } catch (error) {
                        console.error('删除配置失败:', error);
                        showAlert('删除配置失败', 'danger');
                    }
                }
            });
        });
    </script>
</body>
</html> 